package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.basebean.BasePageData;
import com.cskaoyan.bean.basebean.BaseParam;
import com.cskaoyan.exception.TopicException;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.TopicMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lxy
 * @create 2021-10-18 22:13
 */
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    TopicMapper topicMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public BasePageData query(String title, String subtitle, BaseParam baseParam) {

        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        TopicExample example = new TopicExample();
        TopicExample.Criteria criteria = example.createCriteria();

        if (title != null && !"".equals(title)) {
            criteria.andTitleLike("%" + title + "%");
        }

        if (subtitle != null && !"".equals(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }


        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());

        List<Topic> topics = topicMapper.selectByExample(example);

        return BasePageData.ok(topics);
    }

    @Override
    public void deleteTopic(Topic topic) throws TopicException {
        topic.setDeleted(true);
        int i = topicMapper.updateByPrimaryKeySelective(topic);
        if (i == 0) {
            throw new TopicException();
        }
        return;
    }

    @Override
    public void createTopic(Topic topic) throws TopicException {
        int i = topicMapper.insertSelective(topic);
        if (i == 0) {
            throw new TopicException();
        }
        return;
    }

    @Override
    public void updateTopic(Topic topic) throws TopicException {
        int i = topicMapper.updateByPrimaryKeySelective(topic);
        if (i == 0) {
            throw new TopicException();
        }
        return;
    }

    @Override
    public Topic getReadTopic(Integer id) throws TopicException {

        Topic topic = topicMapper.selectByPrimaryKey(id);
        if (topic == null) throw new TopicException();
        return topic;
    }

    @Override
    public BasePageData<WxTopicList> listTopic(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        TopicExample example = new TopicExample();
        example.createCriteria().andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(example);
        BasePageData basePageData = BasePageData.ok(topics);
        return basePageData;
    }

    @Override
    public TopicDetailVO detail(Integer id) {
        Topic topic = topicMapper.selectByPrimaryKey(id);
        Integer[] goodsIds = topic.getGoods();
        ArrayList<TopicDetailVO.GoodsDTO> goodList = new ArrayList<>();
        for (Integer goodsId : goodsIds) {
            Goods goods1 = goodsMapper.selectByPrimaryKey(goodsId);
            //封装数据
            TopicDetailVO.GoodsDTO goodsDTO = new TopicDetailVO.GoodsDTO();
            goodsDTO.setId(goods1.getId());
            goodsDTO.setBrief(goods1.getBrief());
            goodsDTO.setCounterPrice(goods1.getCounterPrice().intValue());
            goodsDTO.setIsHot(goods1.getIsHot());
            goodsDTO.setIsNew(goods1.getIsNew());
            goodsDTO.setName(goods1.getName());
            goodsDTO.setPicUrl(goods1.getPicUrl());
            goodsDTO.setRetailPrice(goods1.getRetailPrice().intValue());
            goodList.add(goodsDTO);
        }
        TopicDetailVO topicDetailVO = new TopicDetailVO();
        topicDetailVO.setTopic(topic);
        topicDetailVO.setGoods(goodList);
        return topicDetailVO;
    }

    @Override
    public BasePageData<WxRelated> related(Integer id) {
        PageHelper.startPage(1, 4);
        Topic topic = topicMapper.selectByPrimaryKey(id);
        Integer[] goodsIds = topic.getGoods();
        ArrayList<WxRelated> wxRelateds = new ArrayList<>();
        //查询所有专题，如果两个专题中有相同的商品id，则判断它们为相关专题
        List<Topic> topics = topicMapper.selectByExample(null);
        for (Topic topic1 : topics) {
            Integer[] goods = topic1.getGoods();
            //比较两个数组中是否有相同的元素
            boolean b = equalsId(goodsIds, goods);
            if(b){
                //如果为true，封装结果
                WxRelated wxRelated = transformByTopic(topic1);
                wxRelateds.add(wxRelated);
            }
        }
        BasePageData basePageData = BasePageData.ok(wxRelateds);
        return basePageData;
    }

    private WxRelated transformByTopic(Topic topic1) {
        WxRelated wxRelated = new WxRelated();
        wxRelated.setId(topic1.getId());
        wxRelated.setTitle(topic1.getTitle());
        wxRelated.setSubtitle(topic1.getSubtitle());
        wxRelated.setPrice(topic1.getPrice().doubleValue());
        wxRelated.setReadCount(topic1.getReadCount());
        wxRelated.setPicUrl(topic1.getPicUrl());
        wxRelated.setSortOrder(topic1.getSortOrder());
        wxRelated.setAddTime(topic1.getAddTime());
        wxRelated.setUpdateTime(topic1.getUpdateTime());
        wxRelated.setContent(topic1.getContent());
        Integer[] goods = topic1.getGoods();
        GoodsWxRelated[] goodsWxRelateds= new GoodsWxRelated[goods.length];
        for (Integer good : goods) {
            int i = 0;
            Goods goods1 = goodsMapper.selectByPrimaryKey(good);
            GoodsWxRelated goodsWxRelated = new GoodsWxRelated();
            goodsWxRelated.setId(goods1.getId());
            goodsWxRelated.setBrief(goods1.getBrief());
            goodsWxRelated.setName(goods1.getName());
            goodsWxRelated.setPicUrl(goods1.getPicUrl());
            goodsWxRelated.setRetailPrice(goods1.getRetailPrice().doubleValue());
            goodsWxRelateds[i] = goodsWxRelated;
            i++;
        }
        wxRelated.setGoods(goodsWxRelateds);
        return wxRelated;
    }

    private boolean equalsId(Integer[] goodsIds, Integer[] goods) {

        if(goodsIds.length==0&&goods.length==0){
            return true;
        }
        if(goodsIds.length!=0&&goods.length!=0){

            for (int i = 0; i < goodsIds.length; i++) {
                for (int i1 = 0; i1 < goods.length; i1++) {
                    if (goodsIds[i] == goods[i1]) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

}
